package com.meizi.daily.rest;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meizi.core.tool.api.PageParam;
import com.meizi.core.tool.api.PageResult;
import com.meizi.core.tool.api.R;
import com.meizi.daily.biz.ConsumptionBiz;
import com.meizi.daily.entity.Consumption;
import com.meizi.daily.query.ConsumptionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author pc
 */
@RestController
@RequestMapping("consumption")
public class ConsumptionController {
    @Autowired
    private ConsumptionBiz baseBiz;

    @RequestMapping
    public R addConsumption(@RequestBody Consumption consumption){
        baseBiz.addConsumption(consumption);
        return new R().ofSuccess();
    }

    @GetMapping("/page")
    public R page(ConsumptionQuery consumptionQuery, PageParam pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<Consumption> list = baseBiz.selectByQuery(consumptionQuery);
        PageInfo<Consumption> of = PageInfo.of(list);
        return new R().ofSuccess(new PageResult(list, of.getTotal()));
    }
}
